/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.DepartmentDAO;
import exatrez.model.Department;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author wan.fairul
 */
public class DepartManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private DepartmentDAO departmentDAO;

    @Transactional
    public void saveOrUpdate(Department department) {
        departmentDAO.saveOrUpdate(department);
    }

    @Transactional
    public void deleteDepartment(Department department) {
        departmentDAO.delete(department);
    }

    public List<Department> findAll() {
        String query = "SELECT a FROM Department a WHERE a.departmentId is not null  ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }

    public Department findDepartmentName(String departmentName) {
        String query = "SELECT a FROM Department a WHERE a.departmentName = :departmentName  ";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("departmentName", departmentName);
        return (Department) q.uniqueResult();
    }
    
    public Department findNotDepartmentName(String departmentId,String departmentName) {
        String query = "SELECT a FROM Department a WHERE a.departmentId != :departmentId and a.departmentName = :departmentName  ";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("departmentId", departmentId);
        q.setString("departmentName", departmentName);
        return (Department) q.uniqueResult();
    } 
    
    public DepartmentDAO getDepartmentDAO() {
        return departmentDAO;
    }

    public void setDepartmentDAO(DepartmentDAO departmentDAO) {
        this.departmentDAO = departmentDAO;
    }
}
